﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using Log.Log;


namespace FixtureManageSystem
{
    public class MysqlHelper
    {
        static MySqlConnection conn;
        // String sql = $"SELECT * FROM t_admin WHERE id = '{textBox1.Text}' and pwd = '{textBox2.Text}'";

        public static MySqlConnection getConn()
        {
            if (conn == null)
            {
                conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["MysqlConnection"].ConnectionString);
            }
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open(); // 打开数据库
            }
            return conn; // 返回数据库对象
        }

        public static MySqlCommand command(string sql)
        {
            new LogFactory().GetLog("loger").Info(true, sql);
            MySqlCommand cmd = new MySqlCommand(sql, getConn());
            return cmd;
        }
        public  static int Execute(string sql) // 更新操作
        {
            return command(sql).ExecuteNonQuery();
        }
        public static MySqlDataReader read(string sql) // 读取操作
        {
            return command(sql).ExecuteReader();
        }

        public static void DaoClose() // 关闭数据库连接
        {
            conn.Close();
        }
    }
}
